using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library.Web;
using ECommerce.Library;
using eShop.Model;
using eShop.Business;

public partial class controls_wucQuickSearch : BaseControl
{
    private const int _categoryLimit = 20;
    private const int _brandLimit = 20;

    protected void Page_Load(object sender, EventArgs e)
    { 
        if(!IsPostBack)
            LoadData();

        SetScript();
    }

    private void SetScript()
    {
        if (string.IsNullOrEmpty(Request.QueryString["bfilter"]) && string.IsNullOrEmpty(Request.QueryString["cfilter"]))
            ScriptManager.RegisterStartupScript(this, this.GetType(), "showBrandTab", "showBrandTab();", true);
        else if (!string.IsNullOrEmpty(Request.QueryString["bfilter"]))
            ScriptManager.RegisterStartupScript(this, this.GetType(), "showBrandTab", "showBrandTab();", true);
        else if (!string.IsNullOrEmpty(Request.QueryString["cfilter"]))
            ScriptManager.RegisterStartupScript(this, this.GetType(), "showCategoryTab", "showCategoryTab();", true);
    }

    private void LoadData()
    {
        ReadWriteCustomCollection<Manufacturer> brands = null;
        ReadWriteCustomCollection<Category> categories = null;

        if (!string.IsNullOrEmpty(Request.QueryString["cfilter"]))
            categories = CategoryManager.FilterCategory(SiteUtility.StringToFilterType(Request.QueryString["cfilter"]));
        else
            categories = Category.RootCategories;

        if (!string.IsNullOrEmpty(Request.QueryString["bfilter"]))
            brands = ManufacturerManager.FilterBrand(SiteUtility.StringToFilterType(Request.QueryString["bfilter"]));
        else
            brands = ManufacturerManager.ActiveBrands(_brandLimit);  

        if (brands != null && brands.Count>0)
        {
            rptBrands.DataSource = brands;
            rptBrands.DataBind();
            lblNoBrandMessage.Visible = false;
        }
        else
            lblNoBrandMessage.Visible = true;

        if (categories != null && categories.Count>0)
        {
            rptCategory.DataSource = categories;
            rptCategory.DataBind();
            lblNoCategoryMessage.Visible = false;
        }
        else
            lblNoCategoryMessage.Visible = true;
    }
}
